package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import one.stand.mapper.CustomerMapper;
import one.stand.mapper.FollowRecordMapper;
import one.stand.model.CustomerModel;
import one.stand.model.FollowRecordModel;
import one.stand.model.ResultModel;
import one.stand.request.FollowRecordRequest;
import one.stand.response.FollowRecordResponse;
import one.stand.service.FollowRecordService;
import one.stand.util.DateUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
@Service
@Slf4j
public class FollowRecordServiceImpl implements FollowRecordService {
    /**
     * 浏览记录
     */
    @Autowired
    protected FollowRecordMapper followRecordMapper;
    /**
     * 客户关系
     */
    @Autowired
    protected CustomerMapper customerMapper;

    @Override
    public ResultModel<List<FollowRecordResponse>> getFollowList(FollowRecordRequest request) {
        FollowRecordModel inModel = new FollowRecordModel();
        BeanUtils.copyProperties(request, inModel);
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        if (request.getCardId() == null) {
            inModel.setFollowCardId(request.getMyCardId());
            inModel.setUserId(request.getId());
        } else {
            inModel.setCardId(null);
            inModel.setFollowCardId(request.getCardId());
        }
        log.info("{}", JSON.toJSONString(inModel));
        List<FollowRecordModel> modelList = followRecordMapper.getFollowList(inModel);
        List<FollowRecordResponse> responseList = new ArrayList<>();
        PageInfo pageInfo = new PageInfo(modelList);
        pageInfo.getList().forEach(f -> {
            FollowRecordResponse response = new FollowRecordResponse();
            BeanUtils.copyProperties(f, response);
            responseList.add(response);
        });
        return ResultModel.success(responseList);
    }

    @Override
    public ResultModel<Boolean> addFollow(FollowRecordRequest request) {
        if (null == request.getCardId()) {
            return ResultModel.checkParamFail();
        }
        //获取客户公司信息
        CustomerModel customerModel = customerMapper.select(new CustomerModel() {
            {
                //客户名片id
                setCardId(request.getCardId());
                /**员工用户id
                 setAscriptionId(request.getMyCardId().longValue());*/
                //公司id
                setCompanyId(request.getCId());
                setIsDel(0);
            }
        });
        if (null == customerModel) {
            return ResultModel.fail("数据错误");
        }
        FollowRecordModel inModel = new FollowRecordModel();
        BeanUtils.copyProperties(request, inModel);
        //
        if (request.getFollowType() != null && "6".equals(String.valueOf(request.getFollowType()))) {
            inModel.setFollowType(5);
        } else if (null != request.getFollowType()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("data", request.getFollowRemark());
            inModel.setFollowRemark(jsonObject.toJSONString());
        }
        inModel.setCustomerId(customerModel.getCustomerId());
        inModel.setUserId(request.getId());
        inModel.setCreateTime(DateUtil.currentDate());
        inModel.setFollowCardId(request.getMyCardId());
        followRecordMapper.insertSelective(inModel);
        return ResultModel.success();
    }
}
